$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'erpCustomerServieceUser/erpcustomerservieceuser/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '用户id ', name: 'userId', index: 'user_id', width: 80 },
			{ label: '用户名', name: 'username', index: 'username', width: 80 }, 			
			//{ label: '业务员', name: 'type', index: 'type', width: 80 },
			{ label: '是否启用', name: 'delFlag', index: 'del_flag', width: 80,
                formatter: function (value, options, row) {
                    var actions = [];
                    if(!value){
                        actions.push('<label class="iSwitch"> <input type="checkbox"  onclick="vm.updatedisplay('+row.id+')" checked> <i></i></label>');
                    }else{
                        actions.push('<label class="iSwitch"> <input type="checkbox" onclick="vm.updatedisplay('+row.id+')"> <i></i></label>');
                    }
                    return actions.join('  ');
                }
			}
        ],
		viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
        rowList : [25,50,100],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        addVisible:false,
		erpCustomerServieceUser: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
		    vm.addVisible=true;
            //vm.showList = false;
			vm.title = "新增";
			vm.erpCustomerServieceUser = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.erpCustomerServieceUser.id == null ? "erpCustomerServieceUser/erpcustomerservieceuser/save" : "erpCustomerServieceUser/erpcustomerservieceuser/update";
			if(isBlank(vm.erpCustomerServieceUser.username)){
				alert("请输入业务员姓名!");
				return;
			}
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.erpCustomerServieceUser),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "erpCustomerServieceUser/erpcustomerservieceuser/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        updatedisplay:function(id){
            $.get(baseURL + "erpCustomerServieceUser/erpcustomerservieceuser/updateDisplay/"+id, function(r){
                if(r.code == 0){
                    alert('操作成功', function(index){
                        $("#jqGrid").trigger("reloadGrid");
                    });
                }else{
                    alert(r.msg);
                }
            });
		},

		getInfo: function(id){
			$.get(baseURL + "erpCustomerServieceUser/erpcustomerservieceuser/info/"+id, function(r){
                vm.erpCustomerServieceUser = r.erpCustomerServieceUser;
            });
		},
		reload: function (event) {
			vm.addVisible=false;
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});